import { Dispatch, ReactNode, SetStateAction, useState } from 'react';
import { createContext } from 'use-context-selector';
import { useEventEmitter, useRequest } from 'ahooks';
import { FaqDataset } from '@/types/know/know';

type KnowMarkContextType = {
  selectedDataset: FaqDataset | null;
  setSelectedDataset: (dataset: FaqDataset) => void;
  answer: string;
  setAnswer: Dispatch<SetStateAction<string>>;
  question: string;
  setQuestion: Dispatch<SetStateAction<string>>;
};

export const KnowMarkContext = createContext<KnowMarkContextType>({
  selectedDataset: null,
  setSelectedDataset: () => {},
  answer: '',
  setAnswer: () => {},
  question: '',
  setQuestion: () => {},
});

const KnowMarkContextProvider = ({ children }: { children: ReactNode }) => {
  const [selectedDataset, setSelectedDataset] = useState<FaqDataset | null>(null);
  const [answer, setAnswer] = useState<string>('');
  const [question, setQuestion] = useState<string>('');
  const contextValue: KnowMarkContextType = {
    selectedDataset,
    setSelectedDataset,
    answer,
    setAnswer,
    question,
    setQuestion,
  };

  return <KnowMarkContext.Provider value={contextValue}>{children}</KnowMarkContext.Provider>;
};

export default KnowMarkContextProvider;
